#pragma once
#include"http_client.hpp"
#include"string_ext.hpp"
#include"wifi.hpp"
#include"config.h"

namespace ganix
{
    class RemoteLogger {
    private:
        const String base_uri="/logger";
        String mac;
        String response_buf;
        String server;

        void write_log(String level, String msg) {
            static String keys[3] = {"entity", "level", "msg"};
            String values[3] = {mac, level, msg};
            http::get(server + base_uri + "/put-log/esp8266", response_buf, keys, values, 3);
        }

        void write_param(String keys_str, String values_str) {
            static String keys[3] = {"entity", "keys", "values"};
            String values[3] = {mac, keys_str, values_str};
            http::get(server + base_uri + "/put-param/esp8266", response_buf, keys, values, 3);
        }
    public:
        RemoteLogger() {
        }

        void init(const String &server) {
            this->server = server;
            mac = wifi::get_mac();
        }

        void info(String msg) {
            write_log("info", msg);
        }

        void put_param(String key, String value) {
            write_param(key, value);
        }

        void put_params(String keys[], String values[], int size) {
            write_param(string_ext::join(",", keys, size), string_ext::join(",", values, size));
        }
    };

    namespace singleton
    {
        RemoteLogger _logger;

        void init_logger(const String &server) {
            _logger.init(server);
            /* 不清楚如何改链接器，暂时还无法做声明实现分离
            if (config::is_enabled("wifi")) // 这一步会编译报错
                _logger.init(server);
            else 
                Serial.println("[logger][warn] init failed: wifi not enabled.");
            */
        }

        RemoteLogger& logger() {
            return _logger;
        }
    } // namespace singleton
} // namespace ganix
